﻿
namespace System.Reflection
{
    using System.Collections.Generic;
    using NCP.Infrastructure.Common.Singleton;

    public static class ReflectionFactory
    {
        /// <summary>
        /// 创建属性的访问接口
        /// </summary>
        /// <typeparam name="TEntity">实例类型</typeparam>
        /// <typeparam name="TValue">值类型</typeparam>
        /// <param name="property">属性对象</param>
        /// <param name="isCache">是否把属性的委托加载到缓存</param>
        /// <returns>创建的属性访问接口</returns>
        public static IPropertyAccessor<TEntity, TValue> Create<TEntity, TValue>(PropertyInfo property,
            bool isCache)
        {
            if (isCache)
            {
                var propertyAccessor = SingletonDictionary<PropertyInfo, object>.Instance.GetValue(property);
                if (propertyAccessor == null)
                {
                    propertyAccessor = new PropertyAccessor<TEntity, TValue>(property);
                    SingletonDictionary<PropertyInfo, object>.Instance.SetValue(property, propertyAccessor);
                }
                return propertyAccessor as IPropertyAccessor<TEntity, TValue>;
            }
            return new PropertyAccessor<TEntity, TValue>(property);
        }
    }
}
